package com.example.minefieldkeepercloud.adapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.activity.treatedmachine.TreateMachineAcDetail;
import com.example.minefieldkeepercloud.bean.TreateMachineBean;
import com.example.minefieldkeepercloud.util.TimeUtil;
import com.king.base.adapter.ViewHolderAdapter;
import com.king.base.adapter.ViewHolderRecyclerAdapter;
import com.king.base.adapter.holder.ViewHolder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TreateMachineAdapter extends ViewHolderRecyclerAdapter<TreateMachineBean.DataBean> {
    private Activity context;
    private int type=0;
    public TreateMachineAdapter(Activity context, List<TreateMachineBean.DataBean> listData) {
        super(context, listData);
        this.context = context;
    }


    @Override
    public View buildConvertView(LayoutInflater layoutInflater, int viewType) {
        return layoutInflater.inflate(R.layout.adapter_treatemachine,null);
    }
    @Override
    public void bindViewDatas(ViewHolder holder, final TreateMachineBean.DataBean bean, final int position) {

          holder.setText(R.id.tv_IP,bean.getIP());
          TextView tv=holder.getView(R.id.tv_wtime);

        try {
            holder.setText(R.id.tv_time,TimeUtil.dealDateFormat(bean.getCreatedTime()));

            tv.setText(type==0?getTimeFormatText(TimeUtil.dealDateFormat(bean.getCreatedTime()),""):"已处理");
            tv.setTextColor(type==0?context.getResources().getColor(R.color.red):context.getResources().getColor(R.color.system_color));
            holder.setText(R.id.tv_HandleUserName,type==1? bean.getHandleUserName():"未指派");
            holder.setText(R.id.tv_UpdateTime,type==1?getTimeFormatText(TimeUtil.dealDateFormat(bean.getCreatedTime()),TimeUtil.dealDateFormat(bean.getUpdateTime())):"未处理");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        LinearLayout ly2=holder.getView(R.id.ly_2);
        LinearLayout ly3=holder.getView(R.id.ly_3);
        TextView tv_line3=holder.getView(R.id.tv_line3);
        TextView tv_line4=holder.getView(R.id.tv_line4);
        if(type==0)
        {
            ly2.setVisibility(View.GONE);
            ly3.setVisibility(View.GONE);
            tv_line3.setVisibility(View.GONE);
            tv_line4.setVisibility(View.GONE);
        }
        else
        {
            ly2.setVisibility(View.VISIBLE);
            ly3.setVisibility(View.VISIBLE);
            tv_line3.setVisibility(View.VISIBLE);
            tv_line4.setVisibility(View.VISIBLE);
        }

        holder.setText(R.id.tv_catchMsg, bean.getMacErrInfo());
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent=new Intent(context, TreateMachineAcDetail.class);
                  Bundle bd=new Bundle();
                  bd.putSerializable("data",bean);
                  intent.putExtra("bd",bd);
                  context.startActivity(intent);
              }
          });

    }

    public void setType(int type)
    {
        this.type=type;
    }

    /**
     * 时间差
     *
     * @param
     * @return
     */
    public static String getTimeFormatText(String fromDate,String toDates) {

        String toDate=toDates;

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(toDate.isEmpty())
        {
             Date date = new Date(System.currentTimeMillis());
             toDate=df.format(date);
        }

         try {
            //前的时间
            Date fd = df.parse(fromDate);
            //后的时间
            Date td = df.parse(toDate);
            //两时间差,精确到毫秒
            long diff = td.getTime() - fd.getTime();
            long day = diff / 86400000;                         //以天数为单位取整
            long hour= diff % 86400000 / 3600000;               //以小时为单位取整
            long min = diff % 86400000 % 3600000 / 60000;       //以分钟为单位取整
            long seconds = diff % 86400000 % 3600000 % 60000 / 1000;   //以秒为单位取整
            //天时分秒


            if(day>0&&hour>0&&min>0)
            {
                return day+"天"+hour+"小时"+min+"分";
            }
            else if(day==0&&hour>0&&min>0)
            {
                return hour+"小时"+min+"分";
            }
            else if(day==0&&hour==0&&min>0)
            {
                return min+"分";
            }
            else if(day>0&&hour==0&&min>0)
            {
                return day+"天"+hour+"小时"+min+"分";
            }
            else if(day>0&&hour==0&&min==0)
            {
                return day+"天";
            }
            else if(day>0&&hour>0&&min==0)
            {
                return day+"天"+hour+"小时";
            }
            else if(day==0&&hour>0&&min==0)
            {
                return hour+"小时";
            }
            return day+"天"+hour+"小时"+min+"分";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }
}
